package com.xuecheng.learning.service.impl;

import com.alibaba.fastjson.JSON;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.learning.config.PayNotifyConfig;
import com.xuecheng.learning.service.MyCourseTablesService;
import com.xuecheng.messagesdk.model.po.MqMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ReceivePayNotifyService {

    @Autowired
    MyCourseTablesService myCourseTablesService;

    @RabbitListener(queues = PayNotifyConfig.PAYNOTIFY_QUEUE)
    public void receive(Message message) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //解析消息
        byte[] body = message.getBody();
        String s = new String(body);
        //装成对象
        MqMessage mqMessage = JSON.parseObject(s, MqMessage.class);
        //解析消息内容
        //选课id
        String businessKey1 = mqMessage.getBusinessKey1();
        //订单类型
        String ordertype = mqMessage.getBusinessKey2();
        //学习中心服务只要购买课程类的支付订单的结果
        if (ordertype.equals("60201")){
            //根据消息的内容,更新选课记录,
            boolean b = myCourseTablesService.saveChooseCourseStauts(businessKey1);
            if (!b){
                XueChengPlusException.cast("保存课程记录表失败");


            }
        }




    }
    }

